<?php

class SubscriptionController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        
    }

    public function indexAction()
    {
        echo 'hello';	
       /* $user = new Application_Model_UserMapper();
        $this->view->entries = $user->fetchAll();*/
		
    }
	
	

    public function signAction()
    {
    	$this->_helper->layout->disableLayout();
        $cityMapper = new Application_Model_CityMapper();
		$cities = $cityMapper->fetchAll();
		$citiesArray = array();		
		foreach ($cities as $city) {
			$citiesArray[] = $city->toArray();
		}
        $this->view->components = array('cities' => $citiesArray);
    }
	
	public function updateAction(){
		
		$geolocationUrl = 'https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDetjCkdIcx00YbHZhgJjPN6TfSroKn5HQ';
		
	    $this->_helper->layout()->disableLayout(); 
	    $this->_helper->viewRenderer->setNoRender(true);

		$request = $this->getRequest();
		if (!$request->isPost()){
		 	throw new Exception("Attributes no valid", 500);
		}
		
		$email   = $request->getParam('email');
		$cityId  = $request->getParam('cityId');
		$result = '';
		
		$validator = new Zend_Validate_EmailAddress();
		if ($validator->isValid($email)) {
    		$userMapper = new Application_Model_UserMapper();
			
			try{
	      		$userMapper->authenticate($email,$cityId);
				$result = true;
			}
			catch(Exception $e){
				if (strpos($e->getMessage(),'Duplicate entry') !== false) {
					$result = 'registered';
				}
				throw $e;
			}
		} else {
		    
		    foreach ($validator->getMessages() as $message) {
		        $result .= "$message\n";
			}
		}
		
		$this->_helper->json($result);
		
	}
}

